﻿
var suggestDiv = "search_suggest";      //显示的div id
var tbxKeyWord = "tbxsearch";           //关键字输入id
var lastKey = "";
var selectedItem = "";

function suggest(event, inputObj) {
    event = event ? event : (window.event ? window.event : null);
    var keyCode = event.keyCode;

    if (keyCode == 13) {
        $("#" + suggestDiv).css("display", "none");
        if (selectedItem != "")
            $("#" + tbxKeyWord).val($("#" + selectedItem + " .keyTxt").html());
        GetSearch();
        return;
    }
    showSuggest(inputObj);
}

function inputOnblur() {
    setTimeout(' $("#" + suggestDiv).css("display", "none");', 100);
}

function showSuggest(inputObj) {
    var key = $("#" + tbxKeyWord).val();
    if (key == lastKey)
        return false;
    else
        lastkey = key;

    if (/^\s*$/.test(key)) {
        $("#" + suggestDiv).css("display", "none");
        return false;
    };
    selectedItem = "";
    $.ajax({
        type: 'get',
        url: "AutoSearch.aspx?Action=html&skey=" + encodeURI(key) + "&t=" + new Date().getTime(),
        dataType: 'html',
        cache: false,
        beforeSend: function () { $("#" + suggestDiv).html("Loading..."); $("#" + suggestDiv).css("display", "block"); },
        success: function (result) { BindResult(result); },
        complete: function () { }
    });
}

function BindResult(result) {
    if (result == "") {
        $("#" + suggestDiv).css("display", "none");
    }
    else {
        $("#" + suggestDiv).html(result);
        $("#" + suggestDiv + " div").each(function () {
            $("#" + suggestDiv + " div").hover(
                function () { $(this).addClass("suggestOver"); selectedItem = $(this).attr("id"); },
                function () { $(this).removeClass("suggestOver"); }
            );
            $(this).mousedown(function () {
                $("#" + suggestDiv).css("display", "none");
                if (selectedItem != "") { $("#" + tbxKeyWord).val($("#" + selectedItem + " .keyTxt").html()); }
                GetSearch();
            });
        });
        $("#" + suggestDiv).css("display", "block");
    }
}

function GetSearch() {
    //alert($("#" + tbxKeyWord).val());
}